{% block component_head_analytics %}
    {% block component_head_analytics_gtag %}
        {% if storefrontAnalytics and storefrontAnalytics.isActive() %}
            {% set trackingId = storefrontAnalytics.getTrackingId() %}
            {% set trackingUrl = 'https://www.googletagmanager.com/' ~ (trackingId starts with 'GTM-' ? 'gtm.js?id=' : 'gtag/js?id=') %}

            <script>
                {% block component_head_analytics_gtag_config %}
                    window.gtagActive = true;
                    window.gtagURL = '{{ trackingUrl }}{{ trackingId }}';
                    {# @deprecated tag:v6.8.0 - Global variable `window.controllerName` will be removed, use `window.activeRoute` instead. #}
                    window.controllerName = '{{ controllerName|lower }}';
                    {# @deprecated tag:v6.8.0 - Global variable `window.actionName` will be removed, use `window.activeRoute` instead. #}
                    window.actionName = '{{ controllerAction|lower }}';
                    window.activeRoute = '{{ activeRoute }}';
                    window.trackOrders = '{{ storefrontAnalytics.isTrackOrders() }}';
                    window.gtagTrackingId = '{{ trackingId }}';
                    window.dataLayer = window.dataLayer || [];
                    window.gtagConfig = {
                        'anonymize_ip': '{{ storefrontAnalytics.isAnonymizeIp() }}',
                        'cookie_domain': 'none',
                        'cookie_prefix': '_swag_ga',
                    };

                    function gtag() { dataLayer.push(arguments); }
                {% endblock %}
            </script>
        {% endif %}
    {% endblock %}

    {% block component_head_analytics_gtag_consent %}
        {% if storefrontAnalytics and storefrontAnalytics.isActive() %}
            <script>
                (() => {
                    const analyticsStorageEnabled = document.cookie.split(';').some((item) => item.trim().includes('google-analytics-enabled=1'));
                    const adsEnabled = document.cookie.split(';').some((item) => item.trim().includes('google-ads-enabled=1'));

                    // Always set default consent (only denied is allowed with default)
                    gtag('consent', 'default', {
                        'ad_user_data': 'denied',
                        'ad_storage': 'denied',
                        'ad_personalization': 'denied',
                        'analytics_storage': 'denied'
                    });

                    // Only update consents that were granted
                    const consentUpdate = {};

                    if (adsEnabled) {
                        consentUpdate.ad_user_data = 'granted';
                        consentUpdate.ad_storage = 'granted';
                        consentUpdate.ad_personalization = 'granted';
                    }

                    if (analyticsStorageEnabled) {
                        consentUpdate.analytics_storage = 'granted';
                    }

                    if (Object.keys(consentUpdate).length > 0) {
                        gtag('consent', 'update', consentUpdate);
                    }
                })();
            </script>
        {% endif %}
    {% endblock %}
{% endblock %}
